package com.example.pharmacy.service;

import com.example.pharmacy.mapper.CategoryInfoMapper;
import com.example.pharmacy.mapper.DrugInfoMapper;
import com.example.pharmacy.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-21
 * Time: 17:24
 */
@Service
@Slf4j
public class DrugService {
    @Autowired
    private DrugInfoMapper drugInfoMapper;
    @Autowired
    private CategoryInfoMapper categoryInfoMapper;
    public PageResult<DrugInfo> getDrugsByPage(PageRequest pageRequest) {
        List<DrugInfo> results = drugInfoMapper.selectDrugInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());;
        return new PageResult<>(results, drugInfoMapper.countAll(), pageRequest);
    }
    public DrugInfo getDrugDetail(Integer drugId) {
        return drugInfoMapper.selectDrugById(drugId);
    }
    public List<CategoryResult> getDrugsByCategory() {
        List<CategoryResult> categoryResults = new ArrayList<>();
        List<Category> categories = categoryInfoMapper.selectAllCategory();
        for (Category category :
                categories) {
            CategoryResult categoryResult = new CategoryResult();
            categoryResult.setClassId(category.getClassId());
            categoryResult.setClassName(category.getClassName());
            List<DrugInfo> drugInfos = drugInfoMapper.selectDrugInfoByCategory(category.getClassId());
            categoryResult.setDrugInfos(drugInfos);
            categoryResults.add(categoryResult);
        }
        return categoryResults;
    }
    public Integer addDrug(DrugInfo drugInfo) {
        return drugInfoMapper.addDrug(drugInfo);
    }
    public Integer updateDrug(DrugInfo drugInfo) {
        return drugInfoMapper.updateDrug(drugInfo);
    }
    public List<DrugInfo> selectDrugByName(String drugName) {
        return drugInfoMapper.selectDrugByName(drugName);
    }
}
